package com.supermap.panes.advance;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public abstract class SettingPane extends JPanel {
    protected JButton confirmBtn;
    protected JPanel mainPanel;
    protected JPanel bottomPanel;
    private final String title;

    public SettingPane(String title, Object[] args) {
        this.title = title;
        initializeArgs(args);
        initialize();
    }

    private void initialize() {
        setLayout(new BorderLayout());

        JPanel topPanel = new JPanel();
        topPanel.setBorder(new EmptyBorder(1, 6, 0, 1));
        topPanel.setLayout(new BorderLayout());
        add(topPanel, BorderLayout.NORTH);

        JLabel titleLbl = new JLabel(this.title);
        topPanel.add(titleLbl, BorderLayout.WEST);

        JButton closeBtn = new JButton("\uF0CE");
        Font font = closeBtn.getFont();
        closeBtn.setFont(new Font("Wingdings 2", font.getStyle(), font.getSize() + 4));
        closeBtn.setBorderPainted(false);
        closeBtn.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Container parent = getParent();
                if (parent instanceof JSplitPane) {
                    JSplitPane splitPane = (JSplitPane) parent;
                    splitPane.setRightComponent(null);
                }
                parent = parent.getParent().getParent();
                if (parent instanceof JSplitPane) {
                    JSplitPane splitPane = (JSplitPane) parent;
                    splitPane.setRightComponent(null);
                }
                doClose();
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                closeBtn.setBackground(new Color(196, 43, 28));
                closeBtn.setForeground(Color.WHITE);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                closeBtn.setBackground(topPanel.getBackground());
                closeBtn.setForeground(Color.BLACK);
            }
        });
        closeBtn.setToolTipText("关闭");
        topPanel.add(closeBtn, BorderLayout.EAST);

        confirmBtn = new JButton("确定");
        confirmBtn.addActionListener(e1 -> doConfirm());

        bottomPanel = new JPanel();
        bottomPanel.setLayout(new BorderLayout());
        bottomPanel.add(confirmBtn, BorderLayout.EAST);
        add(bottomPanel, BorderLayout.SOUTH);

        mainPanel = new JPanel();
        mainPanel.setLayout(new FlowLayout());
        add(mainPanel, BorderLayout.CENTER);

        initializeCore();
    }

    protected abstract void initializeArgs(Object[] args);

    protected abstract void initializeCore();

    protected abstract void doConfirm();

    protected abstract void doClose();
}
